<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2018/12/29
 * Time: 17:06
 */
namespace app\province\controller;
use think\Db;
class Notice extends Base {
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $map['n_user_id'] = session('p_uid');

            $count = Db::table('up_notice')->alias('a')
                ->where($map)
                ->count();//计算总页面
            $list = Db::table('up_notice')
                ->alias ('a')
                ->where($map)
                ->page($page, $limit)
                ->order('a.n_id  desc')
                ->select();
            foreach ($list as $key=>&$vo){
                    $p_username = Db::table('up_province')->where('p_id',$vo['n_user_id'])->value('p_username');
                    $vo['b_user'] =$p_username;
            }
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        return $this->fetch();
    }

    /*****
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $id = input('param.id');
        $res=  Db::table('up_notice')->where('n_id',$id)->delete();
        if($res){
            return json(['code' => 200, 'data' => '', 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'data' => '', 'msg' => '删除失败']);
        }
    }

    /********
     * @return mixed
     * 添加
     */
    public function add(){
        if(request()->isPost()){
            $publish = input('param.n_publish_time');//1是  2否
            if($publish==1){
                $data['n_status'] = 0;
                $data['n_publish_time'] = input('param.start');
                if($data['n_publish_time']==''||$data['n_publish_time']==null){
                    return json(['code' => 100, 'data' => '', 'msg' => '发布时间不能为空']);
                }else{
                    if($data['n_publish_time']<date('Y-m-d H:i:s',time())){
                        return json(['code' => 100, 'data' => '', 'msg' => '发布时间不能小于当前时间']);
                    }
                }
            }else{
                $data['n_status'] = 1;
                $data['n_publish_time'] = date('Y-m-d H:i:s',time());
            }
            $data['n_name'] = input('param.n_name');
            $data['n_content'] = input('param.n_content');
            if(mb_strlen($data['n_content'],'UTF8')>100){
                return json(['code' => 100, 'data' => '', 'msg' => '消息内容最多可以输入100字符']);
            }
            $data['n_time'] = date('Y-m-d H:i:s',time());
            $data['n_user_id'] = session('p_uid');
            $res = Db::table('up_notice')->insertGetId($data);
            if($res){
                if($publish == 2){//立刻发送，则推送
                    $m_ids = Db::table('up_factory_member')
                        ->column('m_id');
                    $list = [];
                    foreach($m_ids as $k => $val){
                        $v = (string)$val;
                        array_push($list, $v);
                    }
                    try{
                        $this->Jpush1(2, $list, '消息发布', 1);
                    }catch(\Exception $e){
            
                    }
                }
                return json(['code' => 200, 'data' =>"", 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }


    public function update(){
        if(request()->isPost()){
            $publish = input('param.n_publish_time');//1是  2否
            $id= input('param.id');
            if($publish==1){
                $data['n_status'] = 0;
                $data['n_publish_time'] = input('param.start');
                if($data['n_publish_time']==''||$data['n_publish_time']==null){
                    return json(['code' => 100, 'data' => '', 'msg' => '发布时间不能为空']);
                }else{
                    if($data['n_publish_time']<date('Y-m-d H:i:s',time())){
                        return json(['code' => 100, 'data' => '', 'msg' => '发布时间不能小于当前时间']);
                    }
                }
            }else{
                $data['n_status'] = 1;
                $data['n_publish_time'] = date('Y-m-d H:i:s',time());
            }
            $data['n_name'] = input('param.n_name');
            $data['n_content'] = input('param.n_content');
            if(mb_strlen($data['n_content'],'UTF8')>100){
                return json(['code' => 100, 'data' => '', 'msg' => '消息内容最多可以输入100字符']);
            }
//            $data['n_time'] = date('Y-m-d H:i:s',time());
            $data['n_sheng'] = 0;
            $data['n_user_id'] = session('p_uid');
            $res = Db::table('up_notice')->where('n_id',$id)->update($data);
            if($res){
                if($publish == 2){//立刻发送，则推送
                    $m_ids = Db::table('up_factory_member')
                        ->column('m_id');
                    $list = [];
                    foreach($m_ids as $k => $val){
                        $v = (string)$val;
                        array_push($list, $v);
                    }
                    try{
                        $this->Jpush1(2, $list, '消息发布', 1);
                    }catch(\Exception $e){

                    }
                }
                return json(['code' => 200, 'data' =>"", 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '编辑失败']);
            }
        }
        $id = input('param.id');
        $res= Db::table('up_notice')->where('n_id',$id)->find();
        $this->assign('res',$res);
        return $this->fetch();
    }
}